/*
 * Graph.java
 *
 * Created on 4 de enero de 2007, 09:14 AM
 */

package shortestPath;

/**
 *
 * @author tavobula
 */
public class Graph {
    private int Vcnt, Ecnt;
    private boolean digraph;
    private Edge adj[][];
    
    Graph(int V, boolean flag){
        Vcnt=V;
        Ecnt=0;
        digraph=flag;
        adj=new Edge[V][V];
    }
    
    int V(){return Vcnt;}
    int E(){return Ecnt;}
    
    boolean directed(){
        return digraph;
    }
    
    void insert(Edge  e){
        int v=e.getV();
        int w=e.getW();
        if(adj[v][w]==null)Ecnt++;
        adj[v][w]=e;
        if(!digraph)adj[w][v]=e;
    }
    
    void remove(Edge e){
        int v=e.getV(), w=e.getW();
        if(adj[v][w]==null)Ecnt--;
        adj[v][w]=null;
        if(!digraph)adj[w][v]=null;
    }
    
    Edge edge(int v, int w){
        return adj[v][w];
    }
    
    AdjList getAdjList(int v){
        return new AdjArray(v);
    }
    
    private class AdjArray implements AdjList {
        private int i,v;
        
        AdjArray(int v){
            this.v=v;
            this.i=-1;
        }
        
        public Edge  beg(){
            i=-1;
            return nxt();
        }
        
        public Edge nxt(){
            for(i++;i<V();++i)
                if(edge(v,i)!=null) return edge(v,i);
            return null;
        }
        
        
        public boolean end(){
            return i>=V();
        }
        
    }
    
}
